package com.eshipenet.shipnet.commplanrpc.service;


import org.apache.ignite.services.Service;
import org.apache.ignite.services.ServiceContext;
import org.jetbrains.annotations.NotNull;

public class CommPlanIdGeneratorImpl implements Service, CommPlanIdGenerator {

    private Hashids hasher;
    private SnowFlake snowFlake;

    public CommPlanIdGeneratorImpl(long dataCenterId, long machineId) {
        this.hasher = new Hashids("COMMPLAN", 16);
        this.snowFlake = new SnowFlake(dataCenterId, machineId);
    }

    @Override
    public void init(ServiceContext ctx) {
//        this.seq = ignite.atomicSequence(sequenceName, initialValue, create);
    }

    @Override
    public void execute(ServiceContext ctx) {

    }

    @Override
    public void cancel(ServiceContext ctx) {}

    @Override
    @NotNull
    public String generate() {
        long num = this.snowFlake.nextId();
        String hex = String.format("%016x", num);
        return this.hasher.encodeHex(hex);
    }
}
